package com.example.administrator.helloworld;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/3/26 0026.
 */
public class ChatActivity extends Activity {
    private EditText inputText;
    private Button send;

    private ListView msgListView;
    private ChatAdapter adapter;
    private List<Message> messageList = new ArrayList<Message>();
    private int count=0;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.chat);//关联layout

        //initMsgs();
        adapter = new ChatAdapter(this,getMyData());//msg_item
        msgListView = (ListView) findViewById(R.id.msg_list_view);
        msgListView.setAdapter(adapter);
        inputText = (EditText)findViewById(R.id.input_text);
        send = (Button)findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = inputText.getText().toString();
                if(!"".equals(content)&&count%2==0){//若content非空
                    //Message message = new Message(content,Message.TYPE_SENT);
                    Message message = new Message();
                    message.setType(ChatAdapter.VALUE_LEFT_TEXT);
                    message.setValue(content);
                    messageList.add(message);
                    adapter.notifyDataSetChanged();//adapter自带 当有新消息时刷新ListView中的显示
                    msgListView.setSelection(messageList.size());//将ListView定位到最后一行?
                    inputText.setText("");//清空输入框中内容
                    count++;
                }else if(!"".equals(content)&&count%2==1){//若content非空
                    //Message message = new Message(content,Message.TYPE_RECEIVED);
                    Message message = new Message();
                    message.setType(ChatAdapter.VALUE_RIGHT_TEXT);
                    message.setValue(content);
                    messageList.add(message);
                    adapter.notifyDataSetChanged();//adapter自带 当有新消息时刷新ListView中的显示
                    msgListView.setSelection(messageList.size());//将ListView定位到最后一行
                    inputText.setText("");//清空输入框中内容
                    count++;
                }
            }
        });
        msgListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Message message = getMyData().get(position);
                Toast.makeText(ChatActivity.this, message.getValue(),Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

    /*private void initMsgs() {
        Message msg1 = new Message("习习................",Message.TYPE_RECEIVED);
        messageList.add(msg1);
        Message msg2 = new Message("蛤蛤................",Message.TYPE_SENT);
        messageList.add(msg2);
        Message msg3 = new Message("胡搞................",Message.TYPE_RECEIVED);
        messageList.add(msg3);
        Message msg4 = new Message("毛搞................",Message.TYPE_SENT);
        messageList.add(msg4);
    }*/
    /*private BaseAdapter getAdapter(){

        return new ChatAdapter(this, getMyData());
    }*/

    private List<Message> getMyData(){

        //List<Message> messageList = new ArrayList<Message>();
        Message message;
        message = new Message();
        message.setType(ChatAdapter.VALUE_LEFT_TEXT);
        message.setValue("100");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_TIME_TIP);
        message.setValue("2012-12-23 下午2:23");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_RIGHT_TEXT);
        message.setValue("99");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_LEFT_TEXT);
        message.setValue("98");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_TIME_TIP);
        message.setValue("2012-12-23 下午2:25");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_RIGHT_TEXT);
        message.setValue("97");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_RIGHT_TEXT);
        message.setValue("96");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_LEFT_TEXT);
        message.setValue("95");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_TIME_TIP);
        message.setValue("2012-12-23 下午3:25");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_RIGHT_TEXT);
        message.setValue("94");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_LEFT_IMAGE);
        message.setValue("93");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_LEFT_TEXT);
        message.setValue("92");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_LEFT_TEXT);
        message.setValue("91");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_LEFT_IMAGE);
        message.setValue("0");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_RIGHT_TEXT);
        message.setValue("1");
        messageList.add(message);


        message = new Message();
        message.setType(ChatAdapter.VALUE_LEFT_IMAGE);
        message.setValue("2");
        messageList.add(message);


        message = new Message();
        message.setType(ChatAdapter.VALUE_LEFT_TEXT);
        message.setValue("3");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_LEFT_AUDIO);
        message.setValue("4");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_LEFT_TEXT);
        message.setValue("5");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_RIGHT_TEXT);
        message.setValue("6");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_LEFT_TEXT);
        message.setValue("7");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_RIGHT_TEXT);
        message.setValue("8");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_RIGHT_IMAGE);
        message.setValue("9");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_LEFT_TEXT);
        message.setValue("10");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_RIGHT_TEXT);
        message.setValue("11");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_LEFT_TEXT);
        message.setValue("12");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_RIGHT_TEXT);
        message.setValue("13");
        messageList.add(message);

        message = new Message();
        message.setType(ChatAdapter.VALUE_LEFT_TEXT);
        message.setValue("14");
        messageList.add(message);
        return messageList;

    }
}
